package com.shambles.manage2.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.CattleService;
import com.shambles.manage2.service.SysUserService;
import com.shambles.manage2.tkmapper.entity.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * 种类列表接口
 */
@RestController
@RequestMapping("cattle")
public class CattleController {
    @Autowired
    private CattleService cattleService;


    /**
     * 修改种类信息
     */
    @PostMapping("updateCattle")
    RestResponse updateCattle(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        Cattle cattle = new Cattle();
        BeanUtils.populate(cattle,jsonObject);
        this.cattleService.updateCattle(cattle);
        return RestResponse.ok();
    }

    /**
     * 添加种类信息
     */
    @PostMapping("insertCattle")
    RestResponse insertCattle(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        Cattle cattle = new Cattle();
        BeanUtils.populate(cattle,jsonObject);
        this.cattleService.insertCattle(cattle);
        return RestResponse.ok();
    }

    /**
     * 分页查询种类信息
     */
    @PostMapping("queryCattle")
    public RestResponse queryLog(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        MessageRequestVM messageRequestVM=new MessageRequestVM();
        BeanUtils.populate(messageRequestVM,jsonObject);
        PageInfo<Cattle> pageInfo =  cattleService.queryCattle(messageRequestVM);
        return RestResponse.ok(pageInfo);
    }
}
